﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
namespace IHISCETeim.DAL
{
    public  class SEMailListDAL
    {
        public static int Add(string MailTo, string Subject, string Body, int EmailType = 0)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand;
            if (EmailType <= 0)
                dbCommand = DB.GetStoredProcCommand("SEMailList_Add", MailTo, Subject, Body);
            else
                dbCommand = DB.GetStoredProcCommand("SEMailList_AddByType", MailTo, Subject, Body, EmailType);
            DB.ExecuteScalar(dbCommand);
            return 1;
            // return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        /// <summary>
        /// 添加邮件发送,用于处理统一处理
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="DB"></param>
        /// <param name="trans"></param>
        /// <returns></returns>
        public static int Add(SEMailListInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand;
            if (obj.EmailType <= 0)
                dbCommand = DB.GetStoredProcCommand("SEMailList_Add", obj.MailTo, obj.Subject, obj.Body);
            else
                dbCommand = DB.GetStoredProcCommand("SEMailList_AddByType", obj.MailTo, obj.Subject, obj.Body, obj.EmailType);
            
            DB.ExecuteScalar(dbCommand);
            return 1;
        }
      
        /// <summary>
        /// 添加程序错误
        /// </summary>
        /// <param name="errorsoure"></param>
        /// <param name="errormsg"></param>
        public static void AddAppError(string errormsg)
        {
            Add("yeyanxin@ihisce.com", "IHISCE信息平台数据库执行程序错误", errormsg);
        }
        public static IDataReader GetAllNeedSendList(int EmailType = 0)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SEMailList_GetAllNeedSendList", EmailType);
            return DB.ExecuteReader(dbCommand);
	    }
        public static bool UpdateSend(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SEMailList_UpdateSend", ID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
